const path = require("path");
const webpack = require("webpack");
const { VueLoaderPlugin } = require("vue-loader");
const CopyWebpackPlugin = require("copy-webpack-plugin");
const HtmlWebpackPlugin = require("html-webpack-plugin");

/**
 * @type { import('webpack').Configuration }
 */
const config = {
  // context: path.resolve(__dirname, ""),
  entry: "./src/main.js",
  output: {
    filename: `[hash:8].bundle.js`,
    path: path.resolve(__dirname, "dist"),
  },
  resolve: {
    extensions: [".vue", ".js", ".less"],
  },
  module: {
    rules: [
      {
        test: /.vue$/,
        use: ["vue-loader"],
      },
      {
        test: /\.js$/,
        loader: "babel-loader",
        exclude: /node_modules/,
      },
      {
        test: /\.less$/,
        use: ["style-loader", "css-loader", "less-loader"],
      },
      {
        test: /.css$/,
        use: ["style-loader", "css-loader"],
      },
      {
        test: /\.(png|jpe?j|gif|svg)(\?.*)?$/,
        use: [
          {
            loader: "url-loader",
            options: {
              limit: 10000,
              name: "[name].[ext]?[hash]",
              esModule: false // fix <img src="[object Module]">
            },
          },
        ],
      },
    ],
  },
  plugins: [
    new VueLoaderPlugin(),
    new webpack.DefinePlugin({ BASE_URL: "'public/'" }),
    new CopyWebpackPlugin({
      patterns: [
        {
          from: path.resolve(__dirname, "public/favicon.ico"),
          to: path.resolve(__dirname, "dist/public"),
        },
      ],
    }),
    new HtmlWebpackPlugin({
      template: path.resolve(__dirname, "public/index.html"),
      title: "拉钩教育",
    }),
  ],
};

module.exports = config;
